package com.zzyl.service.impl;

import com.zzyl.constant.Constants;
import com.zzyl.dto.UserLoginRequestDto;
import com.zzyl.entity.Member;
import com.zzyl.mapper.MemberMapper;
import com.zzyl.properties.JwtTokenManagerProperties;
import com.zzyl.service.MemberService;
import com.zzyl.service.WechatService;
import com.zzyl.utils.JwtUtil;
import com.zzyl.utils.ObjectUtil;
import com.zzyl.utils.StringUtils;
import com.zzyl.vo.LoginVo;
import org.assertj.core.util.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@Service
public class MemberServiceImpl implements MemberService {

    @Autowired
    private WechatService wechatService;
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private JwtTokenManagerProperties jwtTokenManagerProperties;

    static ArrayList DEFAULT_NICKNAME_PREFIX = Lists.newArrayList(
            "生活更美好",
            "大桔大利",
            "日富一日",
            "好柿开花",
            "柿柿如意",
            "一椰暴富",
            "大柚所为",
            "杨梅吐气",
            "天生荔枝"
    );



    /**
     * 小程序登录逻辑
     * @param userLoginRequestDto
     * @return
     */
    @Override
    public LoginVo login(UserLoginRequestDto userLoginRequestDto) {
        //根据临时凭证code获取openid
        String openid = wechatService.getOpenid(userLoginRequestDto.getCode());
        //根据openid查询用户
        Member member = memberMapper.getByOpenId(openid);
        //如果用户不存在，则创建用户
        if (ObjectUtil.isEmpty(member)) {
            member = Member.builder().openId(openid).build();
        }
        //调用api获取手机号登录凭证
        String phoneCode = wechatService.getPhone(userLoginRequestDto.getPhoneCode());
        //保存或者修改用户
        saveOrUpdate(member, phoneCode);
        //将用户ID存入token中
        Map<String,Object> claims = new HashMap<>();
        claims.put(Constants.JWT_USERID,member.getId());
        claims.put(Constants.JWT_USERNAME,member.getName());
        String token = JwtUtil.createJWT(jwtTokenManagerProperties.getBase64EncodedSecretKey(), jwtTokenManagerProperties.getTtl(),
                claims);
        LoginVo loginVo = new LoginVo();
        loginVo.setToken(token);
        loginVo.setNickName(member.getName());
        return loginVo;
    }




    /**
     * 保存或修改用户
     * @param member
     * @param phone
     */
    private void saveOrUpdate(Member member, String phone) {
       //判断的手机号与数据库中保存的手机号是否一致
        if(ObjectUtil.notEqual(phone,member.getPhone())){
            //设置手机号
            member.setPhone(phone);
        }
        //判断ID是否存在
        if(ObjectUtil.isNotEmpty(member.getId())){
            memberMapper.update(member);
            return;
        }
        //设置默认昵称
        String nickname = DEFAULT_NICKNAME_PREFIX.get((int)Math.random() * DEFAULT_NICKNAME_PREFIX.size()) +
                StringUtils.substring(member.getPhone(),7);
        member.setName(nickname);
        memberMapper.save(member);
    }
}
